<template>
  <div class="login_page fillcontain">
    <transition name="form-fade" mode="in-out">
      <section class="form_contianer" v-show="showLogin">
        <div class="manage_tip">
          <p>极简词条翻译管理系统</p>
        </div>
        <el-form :model="loginForm" :rules="rules" ref="loginForm">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="用户名"
              ><span>dsfsf</span></el-input
            >
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              placeholder="密码"
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="submitForm('loginForm')"
              class="submit_btn"
              >登陆</el-button
            >
          </el-form-item>
        </el-form>
      </section>
    </transition>
  </div>
</template>

<script>
import { adminLogin } from "../api/getData";
import * as commonConst from "../common/commonConst";
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      },
      showLogin: false
    };
  },
  mounted() {
    this.showLogin = true;
    // if (!this.adminInfo.id) {
    // 	//this.getAdminData()
    // }
    //   let usersession = localStorage.getItem(commonConst.userSessionKey);
    //   		if (!usersession) {
    // 		//localStorage.setItem('ms_username',"jahhjsajds接电话时间活动计划书")
    // }
  },
  computed: {
    // ...mapState(['adminInfo']),
  },
  methods: {
    // ...mapActions(['getAdminData']),
    async submitForm(formName) {
      //跳转到管理页面
      // //保存用户session
      // localStorage.setItem(commonConst.userSessionKey, "sssss");
      // localStorage.setItem(commonConst.userNameKey, "hament");
      // localStorage.setItem(commonConst.userNickNameKey, "hament");
      // //跳转到管理页面
      // this.$router.push("homeManager");
      // return;

      this.$refs[formName].validate(async valid => {
        if (valid) {
          try {
            const res = await adminLogin({
              username: this.loginForm.username,
              password: this.loginForm.password
            });

            if (res.errorCode == 0 && res.data.session != null) {
              this.$message({
                type: "success",
                message: "登录成功"
              });

              //保存用户session
              localStorage.setItem(
                commonConst.userSessionKey,
                res.data.session
              );
              localStorage.setItem(commonConst.userNameKey, res.data.username);
              localStorage.setItem(
                commonConst.userNickNameKey,
                res.data.nickname
              );
              //跳转到管理页面
              this.$router.push("homeManager");
            } else {
              this.$message({
                type: "error",
                message: res.msg
              });
            }
          } catch (err) {
            this.$message({
              type: "error",
              message: "连接超时"
            });
            console.log("获取失败", err);
          }
        } else {
          // this.$notify.error({
          // 	title: '错误',
          // 	message: '请输入正确的用户名密码',
          // 	offset: 100
          // });
          return false;
        }
      });
    }
  },
  watch: {
    // adminInfo: function (newValue){
    // 	if (newValue.id) {
    // 		this.$message({
    //             type: 'success',
    //             message: '检测到您之前登录过，将自动登录'
    //         });
    // 		this.$router.push('homeManager')
    // 	}
    // }
  }
};
</script>

<style lang="less" scoped>
@import "../style/mixin.less";
.login_page {
  background-color: @dark_bc;
}
.manage_tip {
  position: absolute;
  width: 100%;
  top: -100px;
  left: 0;
  p {
    font-size: 33px;
    color: #fff;
  }
}
.form_contianer {
  .wh(320px, 200px);
  .ctp(320px, 200px);
  padding: 25px;
  border-radius: 5px;
  text-align: center;
  background-color: #fff;
  .submit_btn {
    width: 100%;
    font-size: 16px;
  }
}
.tip {
  font-size: 12px;
  color: red;
}
.form-fade-enter-active,
.form-fade-leave-active {
  transition: all 1s;
}
.form-fade-enter,
.form-fade-leave-active {
  transform: translate3d(0, -50px, 0);
  opacity: 0;
}
</style>
